using System;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.ComponentModel;

namespace TaskVision
{
    public class ShadowPanel : Panel
    {
        // Fields
        private Color m_color;
        private Hashtable m_controlBitmaps = new Hashtable();
        private short m_nAlphaDifference;
        protected byte m_nShadowWidth;

        // Methods
        public ShadowPanel()
        {
            this.SetDefaultValues();
        }

        private Bitmap CalculateControlShadow(Control control)
        {
            Color color;
            int num;
            this.m_nAlphaDifference = (short)(0xff / this.m_nShadowWidth);
            Bitmap image = new Bitmap(control.Width + this.m_nShadowWidth, control.Height + this.m_nShadowWidth);
            Graphics graphics = Graphics.FromImage(image);
            if (control.Region == null)
            {
                color = Color.FromArgb(0, this.m_color);
                Pen pen = new Pen(Color.Empty);
                for (num = 0; num <= (this.m_nShadowWidth - 1); num++)
                {
                    color = Color.FromArgb(color.A + this.m_nAlphaDifference, color);
                    pen.Color = color;
                    graphics.DrawRectangle(pen, (int)(this.m_nShadowWidth + num), (int)(this.m_nShadowWidth + num), (int)((control.Width - (2 * num)) - 1), (int)((control.Height - (2 * num)) - 1));
                }
            }
            else
            {
                color = Color.FromArgb(0, this.m_color);
                SolidBrush brush = new SolidBrush(Color.Empty);
                Region region = control.Region.Clone();
                region.Translate((int)(this.m_nShadowWidth + 1), (int)(this.m_nShadowWidth + 1));
                for (num = 0; num <= (this.m_nShadowWidth - 1); num++)
                {
                    color = Color.FromArgb(color.A + this.m_nAlphaDifference, this.m_color);
                    brush.Color = color;
                    region.Translate(-1, -1);
                    graphics.FillRegion(brush, region);
                }
            }
            graphics.Dispose();
            return image;
        }

        protected override void InitLayout()
        {
            base.InitLayout();
            this.OnLoad();
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            if (e.Control is IPressable)
            {
                (e.Control as IPressable).ePressedChanged += new EventHandler(this.OnPressedChanged);
            }
            e.Control.SizeChanged += new EventHandler(this.OnControlSizeChanged);
            e.Control.VisibleChanged += new EventHandler(this.OnVisibleChanged);
            base.OnControlAdded(e);
        }

        private void OnControlSizeChanged(object sender, EventArgs e)
        {
            this.SetControlShadowBitmap((Control)sender);
        }

        protected virtual void OnLoad()
        {
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            foreach (Control control in base.Controls)
            {
                Bitmap image = (Bitmap)this.m_controlBitmaps[control];
                if (image == null)
                {
                    this.SetControlShadowBitmap(control);
                    image = (Bitmap)this.m_controlBitmaps[control];
                }
                if (control.Visible && (!(control is IPressable) || ((control is IPressable) && !(control as IPressable).bPressed)))
                {
                    e.Graphics.DrawImage(image, control.Bounds.Left, control.Bounds.Top);
                }
            }
            base.OnPaint(e);
        }

        private void OnPressedChanged(object sender, EventArgs e)
        {
            Control control = (Control)sender;
            if ((control as IPressable).bPressed)
            {
                control.Location = new Point(control.Location.X + this.m_nShadowWidth, control.Location.Y + this.m_nShadowWidth);
            }
            else
            {
                control.Location = new Point(control.Location.X - this.m_nShadowWidth, control.Location.Y - this.m_nShadowWidth);
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.Invalidate();
            base.OnSizeChanged(e);
        }

        protected virtual void OnVisibleChanged(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        private void SetControlShadowBitmap(Control control)
        {
            Bitmap bitmap = this.CalculateControlShadow(control);
            this.m_controlBitmaps[control] = bitmap;
        }

        private void SetDefaultValues()
        {
            this.m_nShadowWidth = 3;
            this.BackColor = Color.Transparent;
            this.m_color = Color.DimGray;
            this.Font = new Font("Arial", 10f, FontStyle.Regular);
        }

        // Properties
        [Category("Shadow"), RefreshProperties(RefreshProperties.Repaint), Description("The shadow color next to the control.")]
        public Color ShadowColor
        {
            get
            {
                return this.m_color;
            }
            set
            {
                this.m_color = value;
            }
        }

        [Category("Shadow"), RefreshProperties(RefreshProperties.Repaint), Description("The width of the shadow.")]
        public byte ShadowWidth
        {
            get
            {
                return this.m_nShadowWidth;
            }
            set
            {
                this.m_nShadowWidth = value;
            }
        }
    }

    public interface IPressable
    {
        // Events
        event EventHandler ePressedChanged;

        // Properties
        bool bPressed { get; set; }
    }

    public class istColors
    {
        // Fields
        public static Color Bright = Color.FromArgb(140, 180, 250);
        public static Color Dark = Color.FromArgb(0x55, 0x74, 0xcd);
        private static SolidBrush m_brightBrush;
        private static Pen m_brightPen;
        private static SolidBrush m_darkBrush;
        private static Pen m_darkPen;

        // Properties
        public static SolidBrush BrightBrush
        {
            get
            {
                if (m_brightBrush == null)
                {
                    m_brightBrush = new SolidBrush(Bright);
                }
                return m_brightBrush;
            }
        }

        public static Pen BrightPen
        {
            get
            {
                if (m_brightPen == null)
                {
                    m_brightPen = new Pen(Bright, 1f);
                }
                return m_brightPen;
            }
        }

        public static SolidBrush DarkBrush
        {
            get
            {
                if (m_darkBrush == null)
                {
                    m_darkBrush = new SolidBrush(Dark);
                }
                return m_darkBrush;
            }
        }

        public static Pen DarkPen
        {
            get
            {
                if (m_darkPen == null)
                {
                    m_darkPen = new Pen(Dark, 1f);
                }
                return m_darkPen;
            }
        }
    }
}
